package com.example.web;

import com.example.common.core.Result;
import com.example.common.core.ResultCode;
import com.example.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class DefaultExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public Result<Object> handleRuntimeException(ServiceException e) {
        log.error("ServiceException", e);
        return Result.of(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
    public Result<Object> mediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException e) {
        log.error("HttpMediaTypeNotAcceptableException", e);
        return Result.of(ResultCode.NOT_ACCEPTABLE, e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result<Object> handleRuntimeException(RuntimeException e) {
        log.error("RuntimeException", e);
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result<Object> handleException(Exception e) {
        log.error("Exception", e);
        return Result.error(e.getMessage());
    }

}
